<?php
/*
 * @美潮风
 * @功能说明：API模块通用函数
 * @更新说明：暂无更新
 * @文件名 function.php
 * @编码 UTF-8
 * @创建时间 2015-6-8 下午3:27:46
 * @创建人 XING
 */

/*
 * API接口数据格式化输出
 * @date: 2015-6-8 下午3:29:27
 * @editor: XING
 */
if (!function_exists('dataFormat')) {
    function dataFormat($data = array(), $recode = 1, $msg = '', $adds = array()) {
        header('Content-Type:application/json; charset=utf-8');
        $_arrayErrorMap = array(
            '0'    => '请求参数错误',
            '1'    => '',
            '2'    => '此唯一可匹配设计师,重新匹配失败',
            '3'    => '非法支付渠道',
            '4'    => '更新失败',
            '5'    => '非法的支付订单',
            '6'    => '操作失败',
            '7'    => '支付失败',
            '8'    => '保存失败',
            '1001' => '签名错误',
            '1002' => 'access token 过期或错误',
            '1003' => '非法手机号码',
            '1004' => '流水出错',
            '1005' => '短信发送失败',
            '1006' => '验证码获取失败',
            '1007' => '未添加IP白名单',
            '1008' => '超出当日最大请求次数',
            '1009' => '其它错误',
            '1010' => '手机号码格式错误',
            '1011' => 'access token 参数为空',
            '1012' => '无效请求',
            '1013' => '上一验证码还有效,请求过于频繁',
            '1014' => '验证码有误或过期',
            '1015' => '该手机号码未成功获取验证码',
            '2001' => '服务器繁忙',
            '2006' => '缓存服务繁忙',
        );
        if (!$msg) {
            $msg = $_arrayErrorMap[$recode];
        }
        if ($data || $adds) {
            exit(json_encode(array_merge(array(
                'success' => true,
                'data'    => $data,
                'return_code'    => $recode,
                'return_msg'     => $msg ? $msg : 'success'
            ), $adds), JSON_UNESCAPED_UNICODE));
        } else {
            exit(json_encode(array_merge(array(
                'success' => true,
                'return_code'    => $recode,
                'return_msg'     => $msg ? $msg : '没有相关数据',
                'data'    => array(),
            )), JSON_UNESCAPED_UNICODE));
        }
    }
}
/*
 * 订单生成码
 * @date: 2015-6-4 下午4:51:01
 * @editor: XING
 */
/*if(!function_exists('mkorder')){
	function mkorder(){
		$creatOid = date("Y").substr(time(),4).rand(10000,99999);
		$isExists = M('OrderEntity')->where(array('order_id'=>$creatOid))->count();
		if($isExists > 0)
			self::mkorder();
		else
			return $creatOid;
	}
}*/

/*
 * 敏感词汇过滤
 * @date: 2015-9-9 上午10:59:47
 * @editor: XING
 */
if (!function_exists('wordFilter')) {
    function wordFilter($words) {
        $configSet = getSysConfig('senSitive');
        if ($words && $configSet) {
            foreach (explode(',', $configSet) AS $val) {
                if (strstr($words, $val)) {
                    dataFormat('', 0, '含敏感词汇');
                }
            }
        }
    }
}

/*
 * 美潮风_密钥加密
 * @date: 2016-10-20 上午10:57:58
 * @editor: XING
 */
if(!function_exists('modSercret')){
	function modSercret($cust_id){
		$sekSecret = substr(md5($cust_id.C('ENCKEY')),8,8).$cust_id.(time()+24*3600);
		S('loginSek'.substr(md5($cust_id),3,5),$sekSecret);
		return $sekSecret;
	}
}

/*
 * 美潮风_密钥解密
 * @date: 2016-10-20 上午10:58:33
 * @editor: XING
 */
if(!function_exists('spotSecret')){
	function spotSecret($keys){
		if(substr($keys,-10) <= time()){
			echo json_encode(array(
					'return_code' => '2',
					'return_msg' => '当前登录已过期'
				));exit;
		}
		$lastestKeys = $keys;
		$keys = substr($keys,0,-10);
		if($keys && $keys == substr(md5(substr($keys,8).C('ENCKEY')),8,8).substr($keys,8)){
			$getParamsId = substr($keys,8);
			if(S('loginSek'.substr(md5($getParamsId),3,5)) != $lastestKeys && S('loginSek'.substr(md5($getParamsId),3,5))){
				echo json_encode(array(
						'return_code' => '2',
						'return_msg' => '当前登录已过期'
				));exit;
			}
			return $getParamsId;
		}else{
			echo json_encode(array(
					'return_code' => '1',
					'return_msg' => '非法参数'
				));exit;
		}
	}
}

/*
 * 检测参数合法性
 * @date: 2016-3-23 下午12:02:28
 * @editor: XING
 */
if (!function_exists('forLegal')) {
    function forLegal($sek) {
        if (!$sek) {
            redirect('/saler/noauth?msg=访问出错，请返回~');
        }
    }
}




/*
 * 订单生成码
 * @date: 2015-6-4 下午4:51:01
 * @editor: XING
 */
if(!function_exists('mkorder')){
    function mkorder(){
        $data['var']=time();
        $rands = M('orderNo')->add($data);
        $rand_num=strlen($rands);
        if($rand_num<8){
            $rand_num=strlen($rands);
            $i=$rand_num;
            while ($i<8){
                $rands='0'.$rands;
                $i++;
            }
        }
        
        $creatOid = date("Y").substr(time(),4).$rands;
        $isExists = M('OrderEntity')->where(array('order_id'=>$creatOid))->count();
        if($isExists > 0)
            return mkorder();
        else
            return $creatOid;
    }
}

/*
 * 秒杀订单生成码
 * @date: 2017-10-13 下午4:51:01
 *
 */
if(!function_exists('msorder')){
    function msorder(){

        $data['var']=time();
        $rands = M('orderNo')->add($data);
        $rands=str_pad($rands,8,"0",STR_PAD_LEFT);

        $creatOid = date("Y").substr(time(),4).$rands;
        $creatOid=(string)$creatOid;
        $isExists = M('SeckillOrder')->where(array('order_id'=>$creatOid))->count();
        if($isExists > 0)
            return msorder();
        else
            return $creatOid;
    }
}

/*
 * 秒杀订单生成码
 * @date: 2015-6-4 下午4:51:01
 * @editor: XING
 */
if(!function_exists('msorders')){
    function msorders(){

        $data['var']=time();
        $rands = M('orderNo')->add($data);
        $rand_num=strlen($rands);
        if($rand_num<8){
            $rand_num=strlen($rands);
            $i=$rand_num;
            while ($i<8){
                $rands='0'.$rands;
                $i++;
            }
        }

        $creatOid = date("Y").substr(time(),4).$rands;
        $creatOid=(string)$creatOid;
        $isExists = M('SeckillOrder')->where(array('order_id'=>$creatOid))->count();
        if($isExists > 0)
            return msorder();
        else
            return $creatOid;
    }
}

/*
 * 返回随机字符串
 * @date: 2016-04-26 18:27:00
 * @editor: WENCUN
 * */
if (!function_exists('createStr')) {
    function createStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }

        return $str;
    }
}

/*
 * 对象转数组
 * @date: 2015-3-22下午1:33:12
 * @editor: XING
 */
function objectToArray($e){
	$e=(array)$e;
	foreach($e as $k=>$v){
		if( gettype($v)=='resource' ) return;
		if( gettype($v)=='object' || gettype($v)=='array' )
			$e[$k]=(array)objectToArray($v);
	}
	return $e;
}


/*
 * 上传图片到七牛
 */
function uploadqiniu($ben_img,$name){
    $filePath = UPLOAD_PATH.$ben_img;
    $key = $name.'_'.time().'_'.rand(1000,9999);
    // 要上传文件的本地路径
    $data = (new \Org\Util\Qiniu())->upFile($filePath,$key);
    $qiniu_file_url = QINIUHOST.$data['key'];
    if($qiniu_file_url){
        unlink($filePath);
    }
    return $qiniu_file_url;
}


if(!function_exists('thumbImg')){
    function thumbImg($img_path, $ratio, $is_del = true){
        $image = new \Think\Image();
        $image->open($img_path);
        $width = $image->width(); // 返回图片的宽度
        $height = $image->height();
        $thumb_w = $width * $ratio; //取得图片的长宽比
        $thumb_h = $height * $ratio;
        //如果文件路径不存在则创建
        $save_path_info = pathinfo($img_path);
        $new_name = date("YmdHis") . '_' . rand(10000, 99999);
        if(!is_dir($save_path_info['dirname'])) mkdir ($save_path_info['dirname'], 0777);
        $save_path = $save_path_info['dirname'].'/'.$new_name.'.'.$save_path_info['extension'];
        $image->thumb($thumb_w, $thumb_h)->save($save_path);
        if($is_del) @unlink($img_path); //删除源文件
        return $save_path;
    }
}

/*
 * 初始化变量
 * @date: 2016-05-18 11:31:00
 * @editor: gaochaolyf
 * */
if (!function_exists('initParam')) {
    function initParam($parameter, $value) {
        $val = isset($parameter) ? ($parameter ? $parameter : $value) : $value;

        return $val;
    }
}

if(!function_exists('mySpreadSek')){
    function mySpreadSek($cust_id){
        $sekSecret = substr(md5($cust_id),5,6).$cust_id;
        return $sekSecret;
    }
}

/*
 * 密钥解密
 * @date: 2016-3-10 下午3:50:24
 * @editor: XING
 */
if(!function_exists('spotSek')){
    function spotSek($keys){
        if($keys && $keys == substr(md5(substr($keys,6)),5,6).substr($keys,6)){
            return substr($keys,6);
        }else{
            return false;
        }
    }
}

?>